package com.agileai.bpm.controller.define;

import java.util.List;

import com.agileai.bpm.bizmoduler.define.WfProcessManage;
import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;
import com.agileai.util.ListUtil;
import com.agileai.util.StringUtil;

public class WfProcessManageEditHandler extends StandardEditHandler{
	public WfProcessManageEditHandler(){
		super();
		this.listHandlerClass = WfProcessManageListHandler.class;
		this.serviceId = buildServiceId(WfProcessManage.class);;
	}
	public ViewRenderer prepareDisplay(DataParam param) {
		DataRow record = getService().getRecord(param);
		this.setAttributes(record);
		String operateType = param.get(OperaType.KEY);
		this.setOperaType(operateType);
		User user = (User)this.getUser();
		if (OperaType.CREATE.equals(operateType)){
			this.setAttribute("WFP_ID",KeyGenerator.instance().genKey());
			this.setAttribute("WFP_CREATE_TIME", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL));
			this.setAttribute("WFP_MODIFY_TIME", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL));
			this.setAttribute("WFP_MODIFIER", user.getUserId());
			this.setAttribute("WFP_MODIFIER_NAME", user.getUserName());
			this.setAttribute("WFP_CREATER", user.getUserId());
			this.setAttribute("WFP_CREATER_NAME", user.getUserName());
			this.setAttribute("WFP_GROUP", "testflow");
			String version = DateUtil.getDateByType(4)+"_01";
			this.setAttribute("WFP_VERSION", version);
		}
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	public ViewRenderer doCheckExistWfCodeAction(DataParam param){
		String rspText = "";
		String processCode = param.get("WFP_CODE");
		WfProcessManage wfProcessManage = (WfProcessManage)this.lookupService(WfProcessManage.class);
		DataParam dataParam = new DataParam();
		dataParam.put("WFP_CODE",processCode);
		List<DataRow> records = wfProcessManage.findRecords(dataParam);
		if (!ListUtil.isNullOrEmpty(records)){
			rspText = "exist";
		}
		return new AjaxRenderer(rspText);
	}
	public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		User user = (User)this.getUser();
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);	
		}
		else if (OperaType.UPDATE.equals(operateType)){
			DataRow record = getService().getRecord(param);
			DataParam newParam = record.toDataParam(true);
			newParam.update(param);
			newParam.put("WFP_MODIFY_TIME", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL));
			newParam.put("WFP_MODIFIER", user.getUserId());
			getService().updateRecord(newParam);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	protected void processPageAttributes(DataParam param) {
		FormSelect typeGroupFormSelect = FormSelectFactory.create("WF_GROUP");
		String wfGroup = getAttributeValue("WFP_GROUP");
		if (StringUtil.isNullOrEmpty(wfGroup)){
			wfGroup = param.get("WFP_GROUP");
		}
		typeGroupFormSelect.setSelectedValue(wfGroup);
		this.setAttribute("WFP_GROUP", typeGroupFormSelect);
		FormSelect stateSelect = FormSelectFactory.create("WF_STATE");
		String wfState = getAttributeValue("WFP_STATE");
		if (StringUtil.isNullOrEmpty(wfState)){
			wfState = "unrelease";
		}
		stateSelect.setSelectedValue(wfState);
		this.setAttribute("WFP_STATE", stateSelect);
	}
	protected WfProcessManage getService() {
		return (WfProcessManage)this.lookupService(this.getServiceId());
	}
}
